package com.dzt.androidframework.rxjava;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.utils.JLogKit;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.concurrent.ExecutionException;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 响应式编程，异步，RxJava的使用
 */
public class RxJavaActivity extends ActivityTitle {

	@ViewInject(R.id.iv_img)
	private ImageView ivImg;

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_rx_java;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.menu_15);
		Subscriber<String> subscriber = new Subscriber<String>() {
			@Override
			public void onCompleted() {
				JLogKit.getInstance().i("....onCompleted");
			}

			@Override
			public void onError(Throwable e) {
				JLogKit.getInstance().i("....onError");
			}

			@Override
			public void onNext(String s) {
				JLogKit.getInstance().i("....onNext = " + s);
			}
		};

		Observable observable = Observable.create(new Observable.OnSubscribe<String>() {

			@Override
			public void call(Subscriber<? super String> subscriber) {
				subscriber.onNext("Hello");
				subscriber.onNext("RxJava");
				subscriber.onNext("RxAndroid");
				subscriber.onCompleted();
			}
		});

//		Observable observable1 = Observable.just("Hello", "RxJava", "RxAndroid");
//		String[] words = {"Hello", "RxJava", "RxAndroid"};
//		Observable observable2 = Observable.from(words);
		observable.subscribe(subscriber);

		Observable.just(1, 2, 3, 4)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Action1<Integer>() {
					@Override
					public void call(Integer integer) {
						JLogKit.getInstance().i("integer = " + integer);
					}
				});
	}

	@Event(value = {R.id.btn_load_img, R.id.btn_change})
	private void onViewClick(View v) {
		switch (v.getId()) {
			case R.id.btn_load_img:
				loadImg();
				break;
			case R.id.btn_change:
				change();
				break;
			default:
				break;
		}
	}

	private void change() {
		Observable.just("http://img.my.csdn.net/uploads/201308/31/1377949598_7858.jpg") // 输入类型 String
				.map(new Func1<String, Bitmap>() {
					@Override
					public Bitmap call(String filePath) { // 参数类型 String
						return getBitmapFromUrl(filePath); // 返回类型 Bitmap
					}
				})
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Action1<Bitmap>() {
					@Override
					public void call(Bitmap bitmap) { // 参数类型 Bitmap
						ivImg.setImageBitmap(bitmap);
					}
				});
	}

	private Bitmap getBitmapFromUrl(String url) {
		try {
			return Glide.with(context.getApplicationContext())
					.load(url)
					.asBitmap()
					.centerCrop()
					.into(150, 150)
					.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	private void loadImg() {
		Observable.create(new Observable.OnSubscribe<Bitmap>() {
			@Override
			public void call(Subscriber<? super Bitmap> subscriber) {
				try {
					Bitmap bitmap = Glide.with(context.getApplicationContext())
							.load("http://img.my.csdn.net/uploads/201308/31/1377949614_4199.jpg")
							.asBitmap()
							.centerCrop()
							.into(150, 150)
							.get();
					subscriber.onNext(bitmap);
					subscriber.onCompleted();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}

			}
		})
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<Bitmap>() {

					@Override
					public void onCompleted() {

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onNext(Bitmap s) {
						ivImg.setImageBitmap(s);
					}
				});
	}
}
